package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcCcsq;
import com.ruoyi.gcgl.domain.GcClbxd;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcCcsqService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 出差申请单Controller
 * 
 * @author ruoyi
 * @date 2025-09-01
 */
@Controller
@RequestMapping("/gcgl/ccsq")
public class GcCcsqController extends BaseController
{
    private String prefix = "gcgl/ccsq";

    @Autowired
    private IGcCcsqService gcCcsqService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;

    @RequiresPermissions("gcgl:ccsq:view")
    @GetMapping()
    public String ccsq()
    {
        return prefix + "/ccsq";
    }

    /**
     * 查询出差申请单列表
     */
    @RequiresPermissions("gcgl:ccsq:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcCcsq gcCcsq)
    {
        startPage();
        gcCcsq.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        	gcCcsq.setSqr(getLoginName());
        }
        List<GcCcsq> list = gcCcsqService.selectGcCcsqList(gcCcsq);
        return getDataTable(list);
    }

    /**
     * 导出出差申请单列表
     */
    @RequiresPermissions("gcgl:ccsq:export")
    @Log(title = "出差申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcCcsq gcCcsq)
    {
    	gcCcsq.setDeleteFlag(1);
        List<GcCcsq> list = gcCcsqService.selectGcCcsqList(gcCcsq);
        ExcelUtil<GcCcsq> util = new ExcelUtil<GcCcsq>(GcCcsq.class);
        return util.exportExcel(list, "出差申请单数据");
    }

    /**
     * 新增出差申请单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出差申请单
     */
    @RequiresPermissions("gcgl:ccsq:add")
    @Log(title = "出差申请单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcCcsq gcCcsq){
    	SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
		int year = Integer.parseInt(sdy.format(new Date()));
		GcCcsq l_gcCcsq = new GcCcsq();
		List<GcCcsq> lisCount = gcCcsqService.selectGcCcsqList(l_gcCcsq);
		String no = String.format("%03d", lisCount.size()+1);
		gcCcsq.setOrderNo(year+"-"+no);
    	gcCcsq.setSqr(getLoginName());
    	gcCcsq.setSqrAt(new Date());
    	gcCcsq.setStatus(0);
    	gcCcsq.setDeleteFlag(1);
    	gcCcsq.setCreateUser(getLoginName());
    	gcCcsq.setCreateAt(new Date());
    	gcCcsq.setUpdateUser(getLoginName());
    	gcCcsq.setUpdateAt(new Date());
        return toAjax(gcCcsqService.insertGcCcsq(gcCcsq));
    }
    
    /**
     * 新增保存出差申请单
     */
    @RequiresPermissions("gcgl:ccsq:add")
    @Log(title = "出差申请单", businessType = BusinessType.INSERT)
    @PostMapping("/addSubmit")
    @ResponseBody
    public AjaxResult addSubmit(GcCcsq gcCcsq)
    {
    	SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
		int year = Integer.parseInt(sdy.format(new Date()));
		GcCcsq l_gcCcsq = new GcCcsq();
		List<GcCcsq> lisCount = gcCcsqService.selectGcCcsqList(l_gcCcsq);
		String no = String.format("%03d", lisCount.size()+1);
		gcCcsq.setOrderNo(year+"-"+no);
    	gcCcsq.setSqr(getLoginName());
    	gcCcsq.setSqrAt(new Date());
    	gcCcsq.setStatus(1);
    	gcCcsq.setDeleteFlag(1);
    	gcCcsq.setCreateUser(getLoginName());
    	gcCcsq.setCreateAt(new Date());
    	gcCcsq.setUpdateUser(getLoginName());
    	gcCcsq.setUpdateAt(new Date());
    	gcCcsqService.insertGcCcsq(gcCcsq);
    	
    	try {
    		AliyunSmsUtils.returnSmsTz(zjl);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	
        return toAjax(1);
    }

    /**
     * 修改出差申请单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcCcsq gcCcsq = gcCcsqService.selectGcCcsqById(id);
        mmap.put("gcCcsq", gcCcsq);
        return prefix + "/edit";
    }
    
    /**
     * 修改保存出差申请单
     */
    @Log(title = "出差申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcCcsq gcCcsq)
    {
    	gcCcsq.setUpdateUser(getLoginName());
    	gcCcsq.setUpdateAt(new Date());
        return toAjax(gcCcsqService.updateGcCcsq(gcCcsq));
    }
    
    /**
     * 修改保存出差申请单
     */
    @Log(title = "出差申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/editSubmit")
    @ResponseBody
    public AjaxResult editSubmit(GcCcsq gcCcsq)
    {
    	gcCcsq.setStatus(1);
    	gcCcsq.setUpdateUser(getLoginName());
    	gcCcsq.setUpdateAt(new Date());
    	gcCcsqService.updateGcCcsq(gcCcsq);
    	
    	try {
    		AliyunSmsUtils.returnSmsTz(zjl);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	
        return toAjax(1);
    }

    /**
     * 删除出差申请单
     */
    @RequiresPermissions("gcgl:ccsq:remove")
    @Log(title = "出差申请单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcCcsqService.deleteGcCcsqByIds(ids));
    }
    
    /////总经理审批
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/zjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcCcsq gcCcsq)
    {
        startPage();
        gcCcsq.setDeleteFlag(1);
        gcCcsq.setStatus(1);
        List<GcCcsq> list = gcCcsqService.selectGcCcsqList(gcCcsq);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCcsq obj = new GcCcsq();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcCcsqService.updateGcCcsq(obj);
    		
    		GcCcsq gcCcsq = gcCcsqService.selectGcCcsqById(Long.parseLong(id[i].toString()));
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcCcsq.getSqr());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcCcsq obj = new GcCcsq();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcCcsqService.updateGcCcsq(obj);
    		
    		GcCcsq gcCcsq = gcCcsqService.selectGcCcsqById(Long.parseLong(id[i].toString()));
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcCcsq.getSqr());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/print/{id}")
    public String print(@PathVariable("id") Long id, ModelMap mmap){
    	GcCcsq gcCcsq = gcCcsqService.selectGcCcsqById(id);
        mmap.put("obj", gcCcsq);
        return prefix + "/print";
    }
}